import service from '../utils/axios'
import type { ApiResponse } from '../utils/axios'

// 试卷数据接口
export interface PaperInfo {
  id: number
  name: string
  teacherId: number
  courseId: number
  duration: number
  startTime: string
  endTime: string
  status?: 'draft' | 'published'
  createBy?: string
  createTime?: string
  updateBy?: string
  updateTime?: string
}

// 题目详情接口（试卷中的题目）
export interface PaperQuestionInfo {
  id: number
  type: 'single' | 'multiple' | 'judge' | 'fill' | 'essay'
  content: string
  options?: { [key: string]: string }
  answer: string
  analysis?: string
  difficulty: 'easy' | 'medium' | 'hard'
  score: number
}

// 试卷详情响应接口
export interface PaperDetailResponse {
  paper: PaperInfo
  questions: PaperQuestionInfo[]
}

// 分页查询参数接口
interface PaperQueryParams {
  name?: string
  teacherId?: number
  courseId?: number
  page?: number
  size?: number
}

// 分页查询响应接口
interface PageResult<T> {
  data: T[]
  total: number
  page: number
  size: number
}

// 手动组卷请求接口
export interface ManualPaperRequest {
  paper: {
    name: string
    courseId: number
    duration: number
    startTime: string
    endTime: string
  }
  questionIds: number[]
  scores: number[]
}

// 自动组卷请求接口
export interface AutoPaperRequest {
  paper: {
    name: string
    courseId: number
    duration: number
    startTime: string
    endTime: string
  }
  rule: {
    typeRules: {
      [key: string]: {
        count: number
        score: number
      }
    }
    difficultyDistribution?: {
      easy?: number
      medium?: number
      hard?: number
    }
    totalQuestions?: number
    totalScore?: number
  }
}

/**
 * 获取试卷列表（分页）
 */
export function getPaperList(data: PaperQueryParams): Promise<ApiResponse<PageResult<PaperInfo>>> {
  return service({
    url: '/api/papers/query',
    method: 'post',
    data
  })
}

/**
 * 获取试卷详情（包含题目列表）
 */
export function getPaperById(id: number): Promise<ApiResponse<PaperDetailResponse>> {
  return service({
    url: `/api/papers/${id}`,
    method: 'get'
  })
}

/**
 * 获取当前教师的所有试卷
 */
export function getMyPapers(): Promise<ApiResponse<PaperInfo[]>> {
  return service({
    url: '/api/papers/my',
    method: 'get'
  })
}

/**
 * 根据课程ID获取试卷列表
 */
export function getPapersByCourseId(courseId: number): Promise<ApiResponse<PaperInfo[]>> {
  return service({
    url: `/api/papers/course/${courseId}`,
    method: 'get'
  })
}

/**
 * 手动组卷
 */
export function createManualPaper(data: ManualPaperRequest): Promise<ApiResponse<{ id: number }>> {
  return service({
    url: '/api/papers/manual',
    method: 'post',
    data
  })
}

/**
 * 自动组卷
 */
export function createAutoPaper(data: AutoPaperRequest): Promise<ApiResponse<{ id: number }>> {
  return service({
    url: '/api/papers/auto',
    method: 'post',
    data
  })
}

/**
 * 更新试卷
 */
export function updatePaper(id: number, data: PaperInfo): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}`,
    method: 'put',
    data
  })
}

/**
 * 删除试卷
 */
export function deletePaper(id: number): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}`,
    method: 'delete'
  })
}

/**
 * 更新试卷题目
 */
export function updatePaperQuestions(id: number, data: { questionIds: number[], scores: number[] }): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}/questions`,
    method: 'put',
    data
  })
}

/**
 * 发布试卷（支持指定学生或全部发布）
 */
export function publishPaper(id: number, studentIds?: number[]): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}/publish`,
    method: 'put',
    data: studentIds ? { studentIds } : undefined
  })
}

/**
 * 取消发布试卷
 */
export function unpublishPaper(id: number): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}/unpublish`,
    method: 'put'
  })
}

/**
 * 更新试卷的考试时间
 */
export function updateExamTime(id: number, data: { startTime: string, endTime: string }): Promise<ApiResponse<void>> {
  return service({
    url: `/api/papers/${id}/exam-time`,
    method: 'put',
    data
  })
}


